package com.example.yuejian.model;


import com.example.yuejian.Bean.Club;
import com.example.yuejian.UnitData;
import com.example.yuejian.iface.ClubIface;
import com.example.yuejian.listener.ClubListener;
import com.example.yuejian.service.YuejianService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;



public class ClubModel implements ClubIface {
    private YuejianService service;
    private Retrofit retrofit;

    public ClubModel() {
        retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(UnitData.URL)
                .build();

    }


    @Override
    public void clubData(final ClubListener<Club> listener) {
        service= retrofit.create(YuejianService.class);
        Call<Club> call = service.clublist();
        //Retrofit使用-3
        call.enqueue(new Callback<Club>() {
            @Override
            public void onResponse(Call<Club> call, Response<Club> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    //调用监听事件接口的onResponse方法
                    listener.onResponse(response.body());
                }
                else{
                    listener.onFail("解析错误！");
                }
            }

            @Override
            public void onFailure(Call<Club> call, Throwable t) {
                //调用监听事件接口的onFail方法
                listener.onFail(t.toString());
            }
        });
    }


}
